package com.zmks.jkgj.xuexiao.controller;



import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.zmks.jkgj.core.controller.ABaseController;
import com.zmks.jkgj.core.resp.Resp;
import com.zmks.jkgj.xuexiao.SessionManager;
import com.zmks.jkgj.xuexiao.resp.WebResp;
import com.zmks.jkgj.xuexiao.service.ICrctBusiScoreItemService;
import com.zmks.jkgj.xuexiao.tools.ExcelUtil;
import com.zmks.jkgj.xuexiao.vo.CrctBusiScoreItemVO;






/**
 * 上传控制器
 * @author 江亚宁
 *
 */
@Controller
@RequestMapping
public class UpLoadControllerUI  extends ABaseController {
 

@Autowired
	
	private ICrctBusiScoreItemService crctBusiScoreItemService;	



   //上传APK文件
	
	@RequestMapping("/importxls")
	public Resp addapk( MultipartHttpServletRequest request1,	HttpServletRequest request) {
		WebResp webResp = new WebResp();
	    DateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
		try{
		MultipartFile apk = request1.getFile("Filedata"); 
		String sheet_sch_oid=request1.getParameter("sheet_sch_oid");
		System.out.println(sheet_sch_oid);
	    String filename=apk.getOriginalFilename();
	    String pfx= filename.substring(filename.lastIndexOf(".")).toLowerCase();
	    String upfilename=format.format(new Date())+pfx;
		String path = request.getRealPath("/excel")+"/"+upfilename;;
	   //上传临时文件
          try {
			FileCopyUtils.copy(apk.getBytes(),new File(path));
		 } catch (IOException e1) {
			e1.printStackTrace();
		 }
//		 //学校
//		 String  school_oid = SessionManager.getsession(request).getSchool_oid();
//		  
		 //解析excel
		 List<CrctBusiScoreItemVO> List=	 fenjieexcle(path,sheet_sch_oid);
		  
		 //插入数据
		 for(CrctBusiScoreItemVO  c : List )
		 {
			 crctBusiScoreItemService.save(c);
	 }
	 
		
 	} catch (Exception e) {
	e.printStackTrace();
	}
	//上传文件服务器APK
	   return webResp;
	}
	public List<CrctBusiScoreItemVO>  fenjieexcle(String path,String sheet_sch_oid){
	   ExcelUtil poi = new ExcelUtil();
	   List<List<String>> list = poi.read(path);
	   //List<List<String>> list = poi.read("f://1.xls");
	   List listhead=new ArrayList(); 
	   //解析科目
	   if (list != null) {
		   List<String> cellList = list.get(0);
		    for (int j = 0; j < cellList.size(); j++) {
		     if (j == 0) {
		      //System.out.print(cellList.get(j));
		     } else {
		    	 listhead.add(cellList.get(j));
		       
		     }
		    }
		 }
             List arryList=new ArrayList();
	   //解析数据
  //行数
	   for (int i = 1; i < list.size(); i++) {
		   List<String> cellList = list.get(i);
		    //定位学号
		   String banji=cellList.get(1);
		     String snumber=cellList.get(2);
		  
		   for (int j = 0; j < listhead.size(); j++) {
			
			   String xuehao=snumber;
			   String kemu=listhead.get(j).toString();
			   String fen;
			   try{
				   fen=cellList.get(j+1);   
			   }catch(Exception e)
			   {
				   fen="0";   
			   }
			  
			   CrctBusiScoreItemVO v=new CrctBusiScoreItemVO();
			   
			   v.setSheet_sch_oid(Long.parseLong(sheet_sch_oid));
			   v.setSheet_cla_oid(Long.parseLong(banji));
			  
			   v.setStudent_oid(Long.parseLong(xuehao));
			   v.setSubject(kemu);
			   v.setScore_num(fen);
			   v.setCreate_date(new Date());
			   
			   
			   arryList.add(v);
			System.out.println("学号:"+snumber+","+kemu+":"+fen);
			}
		   
	   }
	return arryList;
	   
	   
	   

	 }
	
}
